<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OsanGroupUser extends Model
{
    use HasFactory;

    protected $table = 'osan_group_user';

    /**
     * @param array $gid
     * @return array
     */
    public static function getUid(array $gid = []): array
    {
        if (empty($gid)) {
            return [];
        }
        $rows = self::query()->select(['uid'])->whereIn('gid', $gid)->get()->toArray();
        return array_column($rows, 'uid');
    }

    /**
     * @param int $gid
     * @param array $uid
     * @return bool
     */
    public static function setGroupUser(int $gid, array $uid): bool
    {
        $list = [];
        foreach ($uid as $val) {
            $list[] = ['gid' => $gid, 'uid' => $val];
        }
        self::query()->where('gid', $gid)->delete();
        return self::query()->insert($list);
    }
}
